import junit.framework.TestCase;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Stream;

/**
 * @Author tyy
 * @Description TODO
 * @Since 2021/10/25
 */
public class ReadFile extends TestCase {

    @Test
    public void testReadFile6() throws IOException {
        String fileName = "E:\\learn\\idea-workspace\\leetcode-java\\src\\test\\java\\doc\\newFile.txt";

        // 带缓冲的流读取，默认缓冲区8k
        try (BufferedReader br = new BufferedReader(new FileReader(fileName))){
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        }

        //java 8中这样写也可以
        try (BufferedReader br = Files.newBufferedReader(Paths.get(fileName))){
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        }

    }
    @Test
    public void testReadFile5() throws IOException {
        String fileName = "E:\\learn\\idea-workspace\\leetcode-java\\src\\test\\java\\doc\\newFile.txt";

        //如果是JDK11用上面的方法，如果不是用这个方法也很容易
        byte[] bytes = Files.readAllBytes(Paths.get(fileName));

        String content = new String(bytes, StandardCharsets.UTF_8);
        System.out.println(content);
    }
    @Test
    public void testReadFile4() throws IOException {
        String fileName = "E:\\learn\\idea-workspace\\leetcode-java\\src\\test\\java\\doc\\newFile.txt";

        // java 11 开始提供的方法，读取文件不能超过2G，与你的内存息息相关
        //String s = Files.readString(Paths.get(fileName));
    }
    @Test
    public void testReadFile3() throws IOException {
        String fileName = "E:\\learn\\idea-workspace\\leetcode-java\\src\\test\\java\\doc\\newFile.txt";

        // 转换成List<String>, 要注意java.lang.OutOfMemoryError: Java heap space
        List<String> lines = Files.readAllLines(Paths.get(fileName),
                StandardCharsets.UTF_8);
        lines.forEach(System.out::println);

    }
    @Test
    public void testReadFile2() throws IOException {
        String fileName = "E:\\learn\\idea-workspace\\leetcode-java\\src\\test\\java\\doc\\newFile.txt";
        Path filePath = Paths.get("c:/temp", "data.txt");
        // 读取文件内容到Stream流中，按行读取
        Stream<String> lines = Files.lines(Paths.get(fileName));

        // 随机行顺序进行数据处理
        lines.forEach(ele -> {
            System.out.println(ele);
        });
    }

    @Test
    public void testReadFile1() throws IOException {
        //文件内容：Hello World|Hello Zimug
        String fileName = "E:\\learn\\idea-workspace\\leetcode-java\\src\\test\\java\\doc\\newFile.txt";

        try (Scanner sc = new Scanner(new FileReader(fileName))) {
            while (sc.hasNextLine()) {  //按行读取字符串
                String line = sc.nextLine();
                System.out.println("line:"+line);
            }
        }

        try (Scanner sc = new Scanner(new FileReader(fileName))) {
            sc.useDelimiter("\\|");  //分隔符
            while (sc.hasNext()) {   //只按分隔符读取字符串,且不按行读取了
                String str = sc.next();
                System.out.println("str:"+str);
            }
        }

        //sc.hasNextInt() 、hasNextFloat() 、基础数据类型等等等等。
        //文件内容：1|2
        fileName = "E:\\learn\\idea-workspace\\leetcode-java\\src\\test\\java\\doc\\newFile5.txt";
        try (Scanner sc = new Scanner(new FileReader(fileName))) {
            sc.useDelimiter("\\|");  //分隔符
            while (sc.hasNextInt()) {   //按分隔符读取Int，且有换行读取的不是int，会直接跳出循环
                int intValue = sc.nextInt();
                System.out.println("intValue:" + intValue);
            }
        }
    }


}
